// export const URL = 'https://yun.shlxkj.info:44310/api'
export const URL = 'https://www.shlxkj.info:44319/API/api'
 // export const URL = 'http://192.168.3.118:9020/api'
 // export const URL = 'https://www.shlxkj.info:9050/ARC1/api'
 // export const URL = 'https://www.shlxkj.info:9050/ARC2/api'
import store from '@/store'
import uuid from '@/js_sdk/com-ls-utils/components/ls-utils/index.js';
const params = (url, data) => {
	url += '?'
	const keys = Object.keys(data)
	for (const key of keys) {
		url += `${key}=${encodeURIComponent(typeof data[key] === 'string' ? data[key].trim() : data[key])}&`
	}
	return url.substring(0, url.length - 1)
}

const request = ({ method, url, data = {} }) => {
	return new Promise((resolve, reject) => {
		console.log('url: ', params(url, data))
		const SerialNo = uuid.createUuid()
		uni.request({
			url: method === 'GET' ? params(url, data) : url,
			method,
			data,
			header: {
				Authorization: store.state.vx_u_info.token,
				ApiKey: store.state.vx_u_info.token,
				SerialNo: SerialNo,
			},
			success: (res) => {
				if (res.statusCode === 200) {
					if (res.data.Code !== 200) {
						let pages = getCurrentPages()
						let current = pages[pages.length - 1]
						if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
							return resolve(res.data)
						}
						reject(res.data)
					}
					resolve(res.data ? res.data : res)
				} else if (res.statusCode === 401) {
					store.commit('$uStore', { name: 'vx_token', value: '' })
					let pages = getCurrentPages()
					let current = pages[pages.length - 1]
					if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
						return resolve(res.data)
					}
					uni.showToast({ title: '需重新登录' })
					uni.navigateTo({ url: '/pages/login/index' })
				} else if (res.code === 401) {
					store.commit('$uStore', { name: 'vx_token', value: '' })
					let pages = getCurrentPages()
					let current = pages[pages.length - 1]
					if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
						return resolve(res.data)
					}
					uni.showToast({ title: '需重新登录' })
					uni.navigateTo({ url: '/pages/login/index' })
				} 
				else {
					uni.showToast({ title: res.data.msg })
					reject(res.data)
				}
				const data = res.data

				if (data) {
					resolve(data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				if (err.statusCode === 401) {
					uni.redirectTo({ url: '/pages/login/index' })
				}
				reject(err)
			}
		})
	})
}

// 登录
export function login (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/GetLogin`,
		data
	})
}

// 获取手机号码
export function getPhone (data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/decryptData`,
		data
		}
	)
}

// 注册
export function register(code) {
	return request({
		method: 'GET',
		url: `${URL}/Wechat/GetLogin?Code=${code}`,
	})
}
export function createUser(data) {
	return request({
		method: 'POST',
		url: `${URL}/User/CreateUser`,
		data
	})
}

// 用户协议
export function getDictionaryByType (data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryByType`,
		data
	})
}

// 解密
export function AESDecrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESDecrypt`,
		data
	})
}

// 加密
export function AESEncrypt (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESEncrypt`,
		data
	})
}

// 初次上报（用户信息）
export function CreateReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}
//二次上报（结果）
export function UpReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpReagentResult`,
		data
	})
}

// 援助
export function UpdateReagentResult (data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpdateReagentResult`,
		data
	})
}

// 获取省市区
export function ConvertCoordinate (data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/ConvertCoordinate`,
		data
	})
}

// 扫码领取电子检测盒
export function Shipment (data) {
	return request({
		method: 'GET',
		url: `${URL}/SelfService/Shipment`,
		data
	})
}

// 获取疾控中心
export function GetCDCVicinity (data) {
	return request({
		method: 'GET',
		url: `${URL}/CDC/GetCDCVicinity`,
		data
	})
}

// 报告过程数据
export function ReportedData (data) {
	return request({
		method: 'POST',
		url: `${URL}/WeChat/ReportedData`,
		data
	})
}
//Base64解码
export function Base64 (data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptMac`,
		data
	})
}

//查询是否存在关联
export function GetReleted(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}

//新建手机号与设备号关联
export function CreateReleted(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}

//9006解密
export function aesDecrypt2(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptYuzhi`,
		data
	})
}
//9005解密结果值
export function aesDecryptjieguozhi(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptJieguozhi`,
		data
	})
}
//获取接口阈值
export function aesDecryptGetAPiyuzhi(data) {
	return request({
		method: 'GET',  
		url: `${URL}/Devicenoyuzhi/getYuzhiBy`,  
		data
	})
}

//查询是已有结果
export function Getresult(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}

//扫码领取设备（base64解密二维码）
export function Base64_theone(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptBase64`,
		data
	})
}

// 扫码领取试剂盒
export function addReceive(data) {
	return request({
		method: 'POST',
		url: `${URL}/Offlinecollection/InsertOfflinecollection`,
		data
	})
}

// 给后端发送订阅消息
export function InsertSubscribeinform(data) {
	return request({
		method: 'POST',
		url: `${URL}/Subscribeinform/InsertSubscribeinform`,
		data
	})
}

// 查询上一次检测结果
export function findLastReagentResult(phone) {
	return request({
		method: 'GET',  
		url: `${URL}/ReagentResult/FindLastReagentResult?phone=${phone}`
	})
}

// 根据手机号查询检测记录
export function GetMacDirectionByPhone(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/GetReagentResultByPhone`,
		data
	})
}

// 创建自评表
export function CreateQuestionnaire(data) {
	return request({
		method: 'POST',
		url: `${URL}/Questionnaire/CreateQuestionnaire`,
		data
	})
}

// 根据手机号查询是否填过自评表
export function GetQuestionnaire(phone) {
	return request({
		method: 'POST',
		url: `${URL}/Questionnaire/GetQuestionnaire?PhoneNumber=${phone}`
	})
}

// 健康档案咨询问题模糊查询
export function Inquire(data){
	return request({
		method: 'POST',
		url: `${URL}/Problem/FuzzyFindByProblem`,
		data
	})
}

// 健康档案咨询根据问题Id查找答案
export function FindAnswer(id){
	return request({
		method: 'POST',
		url: `${URL}/Answer/FindAnswerByProblemId?problemid=${id}`,
	})
}